<div class="region-goods-details row">
	<div class="region-goods-left col-sm-2">库存</div>
	<div class="region-goods-right col-sm-10">
		<div class="form-group">
			<label class="col-sm-1 control-label">编码</label>
			<div class="col-sm-5">
				{ife 'goods' $item}
				<input type="text" name="goodssn" id="goodssn" class="form-control hasoption" value="{$item['goodssn']}" {if $item['hasoption']}readonly{/if}//>
				{else}
				<div class='form-control-static'>{$item['goodssn']}</div>
				{/if}
			</div>

			<label class=" col-sm-1 control-label">条码</label>
			<div class="col-sm-5">
				{ife 'goods' $item}
				<input type="text" name="productsn" id="productsn" class="form-control hasoption" value="{$item['productsn']}" {if $item['hasoption']}readonly{/if}//>
				{else}
				<div class='form-control-static'>{$item['productsn']}</div>
				{/if}
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-1 control-label">重量</label>
			<div class="col-sm-11">
				{ife 'goods' $item}
				<div class="input-group fixsingle-input-group">
					<input type="text" name="weight" id="weight" class="form-control hasoption" value="{$item['weight']}" {if $item['hasoption']}readonly{/if}/>
					<span class="input-group-addon">克</span>
				</div>
				{else}
				<div class='form-control-static'>{$item['weight']} 克</div>
				{/if}
			</div>
		</div>

		<div class="form-group">
			<label class="col-sm-1 control-label">库存</label>
			<div class="col-sm-11">
				{ife 'goods' $item}
				<input type="text" name="total" id="total" class="form-control hasoption" value="{$item['total']}"  style="width:150px;display: inline;margin-right: 20px;" {if $item['hasoption']}readonly{/if}/>
				<label class="checkbox-inline">
					<input type="checkbox" id="showtotal" value="1" name="showtotal" {if $item['showtotal']==1}checked{/if} />显示库存
				</label>
				<span class="help-block">商品的剩余数量, 如启用多规格{if com('virtual')}或为虚拟卡密产品{/if}，则此处设置无效.</span>
				{else}
				<div class='form-control-static'>{$item['total']} 件 {if empty($item['showtotal'])}隐藏库存{else}显示库存{/if}</div>
				{/if}
			</div>
		</div>

		<div class="form-group">
			<label class="col-sm-1 control-label"></label>
			<div class="col-sm-11">
				{ife 'goods' $item}
				{if $item['type'] == 3}
				<label for="totalcnf1" class="radio-inline"><input type="radio" name="totalcnf" value="0" id="totalcnf1" {if empty($item) || $item['totalcnf'] == 0}checked="true"{/if} /> 拍下减库存</label>
				{else}
				<label for="totalcnf1" class="radio-inline"><input type="radio" name="totalcnf" value="0" id="totalcnf1" {if empty($item) || $item['totalcnf'] == 0}checked="true"{/if} /> 拍下减库存</label>
				<label for="totalcnf2" class="radio-inline"><input type="radio" name="totalcnf" value="1" id="totalcnf2"  {if !empty($item) && $item['totalcnf'] == 1}checked="true"{/if} /> 付款减库存</label>
				<label for="totalcnf3" class="radio-inline"><input type="radio" name="totalcnf" value="2" id="totalcnf3"  {if !empty($item) && $item['totalcnf'] == 2}checked="true"{/if} /> 永不减库存</label>
				{/if}
				{else}
				{if $item['type'] == 3}
				<div class='form-control-static'>
					{if empty($item) || $item['totalcnf'] == 0}拍下减库存{/if}
				</div>
				{else}
					<div class='form-control-static'>
						{if empty($item) || $item['totalcnf'] == 0}拍下减库存{/if}
						{if !empty($item) && $item['totalcnf'] == 1}付款减库存{/if}
						{if !empty($item) && $item['totalcnf'] == 2}永不减库存{/if}
					</div>
				{/if}

				{/if}
			</div>
		</div>
	</div>

</div>



<div class="region-goods-details row">
	<div class="region-goods-left  col-sm-2">
		规格
	</div>
	<div class="region-goods-right  col-sm-10">
		<div class="form-group">
			<div class="col-sm-11" style='padding-left:30px;'>
				{ife 'goods' $item}
				<label class="checkbox-inline">
					<input type="checkbox" id="hasoption" value="1" name="hasoption" {if $item['hasoption']==1}checked{/if} />启用商品规格
				</label>
				<span class="help-block">启用商品规格后，商品的价格及库存以商品规格为准,库存设置为0则会到”已售罄“中，手机也不会显示, -1为不限制</span>

				{else}

				<div class='form-control-static'>{if $item['hasoption']==1}启用{else}不启用{/if} </div>

				{/if}
			</div>
		</div>

		<div id='tboption' style="padding-left:15px;{if $item['hasoption']!=1}display:none{/if}" >
			<div class="alert alert-info">
				1. 拖动规格可调整规格显示顺序, 更改规格及规格项后请点击下方的【刷新规格项目表】来更新数据。<br/>
				2. 每一种规格代表不同型号，例如颜色为一种规格，尺寸为一种规格，如果设置多规格，手机用户必须每一种规格都选择一个规格项，才能添加购物车或购买。
			</div>
			<div id='specs'>
				{loop $allspecs $spec}
				{template 'goods/tpl/spec'}
				{/loop}
			</div>
			{ife 'goods' $item}
			<table class="table">
				<tr>
					<td>
						<h4><a href="javascript:;" class='btn btn-primary' id='add-spec' onclick="addSpec()" style="margin-top:10px;margin-bottom:10px;" title="添加规格"><i class='fa fa-plus'></i> 添加规格</a>
							<a href="javascript:;" onclick="refreshOptions();" title="刷新规格项目表" class="btn btn-primary"><i class="fa fa-refresh"></i> 刷新规格项目表</a></h4>
					</td>
				</tr>
			</table>
			{/if}
			<div class="alert alert-info wholesalewarning"  {if $item['type']!=4}  style="display: none"{/if}>
			1. 批发商品设置多规格时,无需设置价格参数(现价,原价,成本价,预售价),当商品保存时会自动获取第一级批发价作为不同规格商品的统一价格!
		</div>
		<div id="options" style="padding:0;">{$html}</div>
	</div>

	<div id="modal-module-chooestemp" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true" style="width:600px;margin:0px auto;">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
					<h3>选择虚拟物品模板</h3>
				</div>
				<div class="modal-body">
					<div class="form-group">
						<label class="col-sm-2 control-label" style='width: 150px'>选择模板:</label>
						<div class="col-sm-9 col-xs-12" style='width: 380px'>
							<select class="form-control tpl-category-parent">
								{loop $virtual_types $virtual_type}
								<option value="{$virtual_type['id']}">{$virtual_type['usedata']}/{$virtual_type['alldata']} | {$virtual_type['title']}</option>
								{/loop}
							</select>
						</div>
					</div>
				</div>
				<div class="modal-footer">
					<span class="btn btn-primary span2" onclick="addtemp()">确认选择</span>
					<a href="#" class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</a>
				</div>
			</div>
		</div>
	</div>
	</div>
</div>
<input type="hidden" name="optionArray" value=''>
<input type="hidden" name="isdiscountDiscountsArray" value=''>
<input type="hidden" name="discountArray" value=''>
<input type="hidden" name="commissionArray" value=''>
<script language="javascript">
	$(function(){
		$(".spec_item_thumb").find('i').click(function(){
			var group  =$(this).parent();
			group.find('img').attr('src',"{MX_SHOP_LOCAL}static/images/nopic100.jpg");
			group.find(':hidden').val('');
			$(this).hide();
			group.find('img').popover('destroy');
		});

			require(['jquery.ui'],function(){
			$('#specs').sortable({
				stop: function(){
					refreshOptions();
				}
			});
			$('.spec_item_items').sortable(
			{
						handle:'.fa-arrows',
						stop: function(){
							refreshOptions();
						}
					}
			);
	    });
		$("#hasoption").click(function(){
			var obj = $(this);
			if (obj.get(0).checked){
				$('.hasoption').attr('readonly',true);
				$("#tboption").show();
				$("#tbdiscount").show();
				$("#isdiscount_discounts").show();
				$("#isdiscount_discounts_default").hide();
				$("#commission").show();
				$("#commission_default").hide();
				$("#discounts_type1").show().parent().show();
				refreshOptions();
			}else{
				$("#tboption").hide();
				refreshOptions();

				$("#isdiscount_discounts").hide();
				var isdiscount_discounts = $("#isdiscount_discounts").html();
				$("#isdiscount_discounts").html('');
				isdiscount_change();
				$("#isdiscount_discounts").html(isdiscount_discounts);

				{if p('commission') && !empty($com_set['level'])}
				$("#commission").hide();
				var commission = $("#commission").html();
				$("#commission").html('');
				commission_change();
				$("#commission").html(commission);
				{/if}

				$("#tbdiscount").hide();
				$("#isdiscount_discounts_default").show();

				$("#commission_default").show();
				$('.hasoption').removeAttr('readonly');

				$("#discounts_type1").hide().parent().hide();
				$("#discounts_type0").click();
			}
		});
	});
	function selectSpecItemImage(obj){
		util.image('',function(val){
			$(obj).attr('src',val.url).popover({
				trigger: 'hover',
				html: true,
				container: $(document.body),
				content: "<img src='" + val.url  + "' style='width:100px;height:100px;' />",
				placement: 'top'
			});

			var group  =$(obj).parent();

			group.find(':hidden').val(val.attachment), group.find('i').show().unbind('click').click(function(){
				$(obj).attr('src',"{MX_SHOP_LOCAL}static/images/nopic100.jpg");
				group.find(':hidden').val('');
				group.find('i').hide();
				$(obj).popover('destroy');
			});
		});
	}
	function addSpec(){
                    var len = $(".spec_item").length;

                    if(type==3 && virtual==0 && len>=1){
						tip.msgbox.err('您的商品类型为：虚拟物品(卡密)的多规格形式，只能添加一种规格！');
                        return;
                    }

					if(type==4 && virtual==0 && len>=2){
						tip.msgbox.err('您的商品类型为：批发商品的多规格形式，只能添加两种规格！');
						return;
					}

					if(type==10 && len>=1){
						tip.msgbox.err('您的商品类型为：话费流量充值，只能添加一种规格！')
						return;
					}

	         $("#add-spec").html("正在处理...").attr("disabled", "true").toggleClass("btn-primary");
		var url = "{php echo webUrl('goods/tpl',array('tpl'=>'spec'))}";
		$.ajax({
			"url": url,
			success:function(data){
				$("#add-spec").html('<i class="fa fa-plus"></i> 添加规格').removeAttr("disabled").toggleClass("btn-primary"); ;
				$('#specs').append(data);
				var len = $(".add-specitem").length -1;
				$(".add-specitem:eq(" +len+ ")").focus();
                                        refreshOptions();
			}
		});
	}
	function removeSpec(specid){
		if (confirm('确认要删除此规格?')){
			$("#spec_" + specid).remove();
			refreshOptions();
		}
	}
	function addSpecItem(specid){
	         $("#add-specitem-" + specid).html("正在处理...").attr("disabled", "true");
		var url = "{php echo webUrl('goods/tpl',array('tpl'=>'specitem'))}" + "&specid=" + specid;
		$.ajax({
			"url": url,
			success:function(data){
				$("#add-specitem-" + specid).html('<i class="fa fa-plus"></i> 添加规格项').removeAttr("disabled");
				$('#spec_item_' + specid).append(data);
				var len = $("#spec_" + specid + " .spec_item_title").length -1;
				$("#spec_" + specid + " .spec_item_title:eq(" +len+ ")").focus();
				refreshOptions
                                                                        if(type==3 && virtual==0){
                                                                                    $(".choosetemp").show();
                                                                         }
			}
		});
	}
	function removeSpecItem(obj){
		$(obj).closest('.spec_item_item').remove();
		refreshOptions();
	}

	function refreshOptions(){
 var html = '<table class="table table-bordered table-condensed"><thead><tr class="active">';
	var specs = [];
         if($('.spec_item').length<=0){
             $("#options").html('');
			 $("#discount").html('');
			 $("#isdiscount_discounts").html('');
			 $("#commission").html('');
			 {if p('commission') && !empty($com_set['level'])}
			 	commission_change();
			 {/if}
			 	isdiscount_change();
             return;
         }
	$(".spec_item").each(function(i){
		var _this = $(this);

		var spec = {
			id: _this.find(".spec_id").val(),
			title: _this.find(".spec_title").val()
		};

		var items = [];
		_this.find(".spec_item_item").each(function(){
			var __this = $(this);
			var item = {
				id: __this.find(".spec_item_id").val(),
				title: __this.find(".spec_item_title").val(),
                                                                        virtual: __this.find(".spec_item_virtual").val(),
				show:__this.find(".spec_item_show").get(0).checked?"1":"0"
			}
			items.push(item);
		});
		spec.items = items;
		specs.push(spec);
	});
	specs.sort(function(x,y){
		if (x.items.length > y.items.length){
			return 1;
		}
		if (x.items.length < y.items.length) {
			return -1;
		}
	});

	var len = specs.length;
	var newlen = 1;
	var h = new Array(len);
	var rowspans = new Array(len);
	for(var i=0;i<len;i++){
		html+="<th>" + specs[i].title + "</th>";
		var itemlen = specs[i].items.length;
		if(itemlen<=0) { itemlen = 1 };
		newlen*=itemlen;

		h[i] = new Array(newlen);
		for(var j=0;j<newlen;j++){
			h[i][j] = new Array();
		}
		var l = specs[i].items.length;
		rowspans[i] = 1;
		for(j=i+1;j<len;j++){
			rowspans[i]*= specs[j].items.length;
		}
	}

	html += '<th><div class=""><div style="padding-bottom:10px;text-align:center;">库存</div><div class="input-group"><input type="text" class="form-control  input-sm option_stock_all"VALUE=""/><span class="input-group-addon"><a href="javascript:;" class="fa fa-angle-double-down" title="批量设置" onclick="setCol(\'option_stock\');"></a></span></div></div></th>';
	html += '<th><div class=""><div style="padding-bottom:10px;text-align:center;">预售价</div><div class="input-group"><input type="text" class="form-control  input-sm option_presell_all"VALUE=""/><span class="input-group-addon"><a href="javascript:;" class="fa fa-angle-double-down" title="批量设置" onclick="setCol(\'option_presell\');"></a></span></div></div></th>';
	html += '<th><div class=""><div style="padding-bottom:10px;text-align:center;">现价</div><div class="input-group"><input type="text" class="form-control  input-sm option_marketprice_all"VALUE=""/><span class="input-group-addon"><a href="javascript:;" class="fa fa-angle-double-down" title="批量设置" onclick="setCol(\'option_marketprice\');"></a></span></div></div></th>';
	html+='<th><div class=""><div style="padding-bottom:10px;text-align:center;">原价</div><div class="input-group"><input type="text" class="form-control  input-sm option_productprice_all"VALUE=""/><span class="input-group-addon"><a href="javascript:;" class="fa fa-angle-double-down" title="批量设置" onclick="setCol(\'option_productprice\');"></a></span></div></div></th>';
	html+='<th><div class=""><div style="padding-bottom:10px;text-align:center;">成本价</div><div class="input-group"><input type="text" class="form-control  input-sm option_costprice_all"VALUE=""/><span class="input-group-addon"><a href="javascript:;" class="fa fa-angle-double-down" title="批量设置" onclick="setCol(\'option_costprice\');"></a></span></div></div></th>';
	html+='<th><div class=""><div style="padding-bottom:10px;text-align:center;">编码</div><div class="input-group"><input type="text" class="form-control  input-sm option_goodssn_all"VALUE=""/><span class="input-group-addon"><a href="javascript:;" class="fa fa-angle-double-down" title="批量设置" onclick="setCol(\'option_goodssn\');"></a></span></div></div></th>';
	html+='<th><div class=""><div style="padding-bottom:10px;text-align:center;">条码</div><div class="input-group"><input type="text" class="form-control  input-sm option_productsn_all"VALUE=""/><span class="input-group-addon"><a href="javascript:;" class="fa fa-angle-double-down" title="批量设置" onclick="setCol(\'option_productsn\');"></a></span></div></div></th>';
	html+='<th><div class=""><div style="padding-bottom:10px;text-align:center;">重量（克）</div><div class="input-group"><input type="text" class="form-control  input-sm option_weight_all"VALUE=""/><span class="input-group-addon"><a href="javascript:;" class="fa fa-angle-double-down" title="批量设置" onclick="setCol(\'option_weight\');"></a></span></div></div></th>';
	html+='</tr></thead>';

	for(var m=0;m<len;m++){
		var k = 0,kid = 0,n=0;
		for(var j=0;j<newlen;j++){
			var rowspan = rowspans[m];
			if( j % rowspan==0){
				h[m][j]={title: specs[m].items[kid].title, virtual: specs[m].items[kid].virtual,html: "<td class='full' rowspan='" +rowspan + "'>"+ specs[m].items[kid].title+"</td>\r\n",id: specs[m].items[kid].id};
			}
			else{
				h[m][j]={title:specs[m].items[kid].title,virtual: specs[m].items[kid].virtual, html: "",id: specs[m].items[kid].id};
			}
			n++;
			if(n==rowspan){
			kid++; if(kid>specs[m].items.length-1) { kid=0; }
			n=0;
			}
		}
	}

	var hh = "";
	for(var i=0;i<newlen;i++){
		hh+="<tr>";
		var ids = [];
		var titles = [];
                                    var virtuals = [];
		for(var j=0;j<len;j++){
			hh+=h[j][i].html;
			ids.push( h[j][i].id);
			titles.push( h[j][i].title);
                           virtuals.push( h[j][i].virtual);
		}
		ids =ids.join('_');
		titles= titles.join('+');

		var val ={ id : "",title:titles, stock : "",presell : "",costprice : "",productprice : "",marketprice : "",weight:"",productsn:"",goodssn:"",virtual:virtuals };
		if( $(".option_id_" + ids).length>0){
			val ={
				id : $(".option_id_" + ids+":eq(0)").val(),
				title: titles,
				stock : $(".option_stock_" + ids+":eq(0)").val(),
				presell : $(".option_presell_" + ids+":eq(0)").val(),
				costprice : $(".option_costprice_" + ids+":eq(0)").val(),
				productprice : $(".option_productprice_" + ids+":eq(0)").val(),
				marketprice : $(".option_marketprice_" + ids +":eq(0)").val(),
                                    goodssn : $(".option_goodssn_" + ids +":eq(0)").val(),
                                    productsn : $(".option_productsn_" + ids +":eq(0)").val(),
				weight : $(".option_weight_" + ids+":eq(0)").val(),
                                  virtual : virtuals
			}
		}

		hh += '<td>'
		hh += '<input data-name="option_stock_' + ids +'" type="text" class="form-control option_stock option_stock_' + ids +'" value="' +(val.stock=='undefined'?'':val.stock )+'"/></td>';
		hh += '<input data-name="option_id_' + ids+'" type="hidden" class="form-control option_id option_id_' + ids +'" value="' +(val.id=='undefined'?'':val.id )+'"/>';
		hh += '<input data-name="option_ids" type="hidden" class="form-control option_ids option_ids_' + ids +'" value="' + ids +'"/>';
		hh += '<input data-name="option_title_' + ids +'" type="hidden" class="form-control option_title option_title_' + ids +'" value="' +(val.title=='undefined'?'':val.title )+'"/></td>';
        hh += '<input data-name="option_virtual_' + ids +'" type="hidden" class="form-control option_virtual option_virtual_' + ids +'" value="' +(val.virtual=='undefined'?'':val.virtual )+'"/></td>';
		hh += '</td>';
		hh += '<td><input data-name="option_presell_' + ids+'" type="text" class="form-control option_presell option_presell_' + ids +'" value="' +(val.presell=='undefined'?'':val.presell )+'"/></td>';
		hh += '<td><input data-name="option_marketprice_' + ids+'" type="text" class="form-control option_marketprice option_marketprice_' + ids +'" value="' +(val.marketprice=='undefined'?'':val.marketprice )+'"/></td>';
		hh += '<td><input data-name="option_productprice_' + ids+'" type="text" class="form-control option_productprice option_productprice_' + ids +'" " value="' +(val.productprice=='undefined'?'':val.productprice )+'"/></td>';
		hh += '<td><input data-name="option_costprice_' +ids+'" type="text" class="form-control option_costprice option_costprice_' + ids +'" " value="' +(val.costprice=='undefined'?'':val.costprice )+'"/></td>';
		hh += '<td><input data-name="option_goodssn_' +ids+'" type="text" class="form-control option_goodssn option_goodssn_' + ids +'" " value="' +(val.goodssn=='undefined'?'':val.goodssn )+'"/></td>';
		hh += '<td><input data-name="option_productsn_' +ids+'" type="text" class="form-control option_productsn option_productsn_' + ids +'" " value="' +(val.productsn=='undefined'?'':val.productsn )+'"/></td>';
		hh += '<td><input data-name="option_weight_' + ids +'" type="text" class="form-control option_weight option_weight_' + ids +'" " value="' +(val.weight=='undefined'?'':val.weight )+'"/></td>';
		hh += "</tr>";
	}
	html+=hh;
	html+="</table>";
	$("#options").html(html);
		refreshDiscount();
		refreshIsDiscount();
		{if p('commission') && !empty($com_set['level'])}
		refreshCommission();
		commission_change();
		{/if}
		isdiscount_change();
}

	function refreshDiscount() {
		var html = '<table class="table table-bordered table-condensed"><thead><tr class="active">';
		var specs = [];

		$(".spec_item").each(function (i) {
			var _this = $(this);

			var spec = {
				id: _this.find(".spec_id").val(),
				title: _this.find(".spec_title").val()
			};

			var items = [];
			_this.find(".spec_item_item").each(function () {
				var __this = $(this);
				var item = {
					id: __this.find(".spec_item_id").val(),
					title: __this.find(".spec_item_title").val(),
					virtual: __this.find(".spec_item_virtual").val(),
					show: __this.find(".spec_item_show").get(0).checked ? "1" : "0"
				}
				items.push(item);
			});
			spec.items = items;
			specs.push(spec);
		});
		specs.sort(function (x, y) {
			if (x.items.length > y.items.length) {
				return 1;
			}
			if (x.items.length < y.items.length) {
				return -1;
			}
		});

		var len = specs.length;
		var newlen = 1;
		var h = new Array(len);
		var rowspans = new Array(len);
		for (var i = 0; i < len; i++) {
			html += "<th>" + specs[i].title + "</th>";
			var itemlen = specs[i].items.length;
			if (itemlen <= 0) {
				itemlen = 1
			}
			;
			newlen *= itemlen;

			h[i] = new Array(newlen);
			for (var j = 0; j < newlen; j++) {
				h[i][j] = new Array();
			}
			var l = specs[i].items.length;
			rowspans[i] = 1;
			for (j = i + 1; j < len; j++) {
				rowspans[i] *= specs[j].items.length;
			}
		}

		{loop $levels $level}
		{if $level['key']=='default'}
		html += '<th><div class=""><div style="padding-bottom:10px;text-align:center;">{$level['levelname']}</div><div class="input-group"><input type="text" class="form-control  input-sm discount_{$level["key"]}_all"VALUE=""/><span class="input-group-addon"><a href="javascript:;" class="fa fa-angle-double-down" title="批量设置" onclick="setCol(\'discount_{$level["key"]}\');"></a></span></div></div></th>';
		{else}
		html += '<th><div class=""><div style="padding-bottom:10px;text-align:center;">{$level['levelname']}</div><div class="input-group"><input type="text" class="form-control  input-sm discount_level{$level['id']}_all"VALUE=""/><span class="input-group-addon"><a href="javascript:;" class="fa fa-angle-double-down" title="批量设置" onclick="setCol(\'discount_level{$level['id']}\');"></a></span></div></div></th>';
		{/if}
		{/loop}
		html += '</tr></thead>';

		for (var m = 0; m < len; m++) {
			var k = 0, kid = 0, n = 0;
			for (var j = 0; j < newlen; j++) {
				var rowspan = rowspans[m];
				if (j % rowspan == 0) {
					h[m][j] = {
						title: specs[m].items[kid].title,
						virtual: specs[m].items[kid].virtual,
						html: "<td class='full' rowspan='" + rowspan + "'>" + specs[m].items[kid].title + "</td>\r\n",
						id: specs[m].items[kid].id
					};
				}
				else {
					h[m][j] = {
						title: specs[m].items[kid].title,
						virtual: specs[m].items[kid].virtual,
						html: "",
						id: specs[m].items[kid].id
					};
				}
				n++;
				if (n == rowspan) {
					kid++;
					if (kid > specs[m].items.length - 1) {
						kid = 0;
					}
					n = 0;
				}
			}
		}

		var hh = "";
		for (var i = 0; i < newlen; i++) {
			hh += "<tr>";
			var ids = [];
			var titles = [];
			var virtuals = [];
			for (var j = 0; j < len; j++) {
				hh += h[j][i].html;
				ids.push(h[j][i].id);
				titles.push(h[j][i].title);
				virtuals.push(h[j][i].virtual);
			}
			ids = ids.join('_');
			titles = titles.join('+');
			var val = {
				id: "",
				title: titles,
				{loop $levels $level}
				{if $level['key']=='default'}
				level{$level['key']}: '',
				{else}
				level{$level['id']}: '',
				{/if}
				{/loop}
				costprice: "",
				presell: "",
				productprice: "",
				marketprice: "",
				weight: "",
				productsn: "",
				goodssn: "",
				virtual: virtuals
			};

			var val ={ id : "",title:titles,{loop $levels $level}{if $level['key']=='default'} level{$level['key']}: '',{else} level{$level['id']}: '',{/if}{/loop}costprice : "",productprice : "",marketprice : "",weight:"",productsn:"",goodssn:"",virtual:virtuals };
			if ($(".discount_id_" + ids).length > 0) {
				val = {
					id: $(".discount_id_" + ids + ":eq(0)").val(),
					title: titles,
					{loop $levels $level}
				{if $level['key']=='default'}
					level{$level['key']}: $(".discount_{$level['key']}_" + ids + ":eq(0)").val(),
				{else}
					level{$level['id']}: $(".discount_level{$level['id']}_" + ids + ":eq(0)").val(),
				{/if}
					{/loop}
					costprice: $(".discount_costprice_" + ids + ":eq(0)").val(),
					presell: $(".discount_presell_" + ids + ":eq(0)").val(),
					productprice: $(".discount_productprice_" + ids + ":eq(0)").val(),
					marketprice: $(".discount_marketprice_" + ids + ":eq(0)").val(),
					presell: $(".discount_presell_" + ids + ":eq(0)").val(),
					goodssn: $(".discount_goodssn_" + ids + ":eq(0)").val(),
					productsn: $(".discount_productsn_" + ids + ":eq(0)").val(),
					weight: $(".discount_weight_" + ids + ":eq(0)").val(),
					virtual: virtuals
				}
			}

			{loop $levels $level}
			hh += '<td>'
			{if $level['key']=='default'}
			hh += '<input data-name="discount_level_{$level['key']}_' + ids +'"type="text" class="form-control discount_{$level['key']} discount_{$level['key']}_' + ids +'" value="' +(val.level{$level['key']}=='undefined'?'':val.level{$level['key']} )+'"/>';
			{else}
			hh += '<input data-name="discount_level_{$level['id']}_' + ids +'"type="text" class="form-control discount_level{$level['id']} discount_level{$level['id']}_' + ids +'" value="' +(val.level{$level['id']}=='undefined'?'':val.level{$level['id']} )+'"/>';
			{/if}
			hh += '</td>';
			{/loop}
			hh += '<input data-name="discount_id_' + ids+'"type="hidden" class="form-control discount_id discount_id_' + ids +'" value="' +(val.id=='undefined'?'':val.id )+'"/>';
			hh += '<input data-name="discount_ids"type="hidden" class="form-control discount_ids discount_ids_' + ids +'" value="' + ids +'"/>';
			hh += '<input data-name="discount_title_' + ids +'"type="hidden" class="form-control discount_title discount_title_' + ids +'" value="' +(val.title=='undefined'?'':val.title )+'"/></td>';
			hh += '<input data-name="discount_virtual_' + ids +'"type="hidden" class="form-control discount_virtual discount_virtual_' + ids +'" value="' +(val.virtual=='undefined'?'':val.virtual )+'"/></td>';
			hh += "</tr>";
		}
		html += hh;
		html += "</table>";
		$("#discount").html(html);
	}

	function refreshIsDiscount() {
		var html = '<table class="table table-bordered table-condensed"><thead><tr class="active">';
		var specs = [];

		$(".spec_item").each(function (i) {
			var _this = $(this);

			var spec = {
				id: _this.find(".spec_id").val(),
				title: _this.find(".spec_title").val()
			};

			var items = [];
			_this.find(".spec_item_item").each(function () {
				var __this = $(this);
				var item = {
					id: __this.find(".spec_item_id").val(),
					title: __this.find(".spec_item_title").val(),
					virtual: __this.find(".spec_item_virtual").val(),
					show: __this.find(".spec_item_show").get(0).checked ? "1" : "0"
				}
				items.push(item);
			});
			spec.items = items;
			specs.push(spec);
		});
		specs.sort(function (x, y) {
			if (x.items.length > y.items.length) {
				return 1;
			}
			if (x.items.length < y.items.length) {
				return -1;
			}
		});

		var len = specs.length;
		var newlen = 1;
		var h = new Array(len);
		var rowspans = new Array(len);
		for (var i = 0; i < len; i++) {
			html += "<th>" + specs[i].title + "</th>";
			var itemlen = specs[i].items.length;
			if (itemlen <= 0) {
				itemlen = 1
			}
			;
			newlen *= itemlen;

			h[i] = new Array(newlen);
			for (var j = 0; j < newlen; j++) {
				h[i][j] = new Array();
			}
			var l = specs[i].items.length;
			rowspans[i] = 1;
			for (j = i + 1; j < len; j++) {
				rowspans[i] *= specs[j].items.length;
			}
		}

		{loop $levels $level}
		{if $level['key']=='default'}
		html += '<th><div class=""><div style="padding-bottom:10px;text-align:center;">{$level['levelname']}</div><div class="input-group"><input type="text" class="form-control  input-sm isdiscount_discounts_{$level['key']}_all"VALUE=""/><span class="input-group-addon"><a href="javascript:;" class="fa fa-angle-double-down" title="批量设置" onclick="setCol(\'isdiscount_discounts_{$level['key']}\');"></a></span></div></div></th>';
		{else}
		html += '<th><div class=""><div style="padding-bottom:10px;text-align:center;">{$level['levelname']}</div><div class="input-group"><input type="text" class="form-control  input-sm isdiscount_discounts_level{$level['id']}_all"VALUE=""/><span class="input-group-addon"><a href="javascript:;" class="fa fa-angle-double-down" title="批量设置" onclick="setCol(\'isdiscount_discounts_level{$level['id']}\');"></a></span></div></div></th>';
		{/if}
		{/loop}
		html += '</tr></thead>';

		for (var m = 0; m < len; m++) {
			var k = 0, kid = 0, n = 0;
			for (var j = 0; j < newlen; j++) {
				var rowspan = rowspans[m];
				if (j % rowspan == 0) {
					h[m][j] = {
						title: specs[m].items[kid].title,
						virtual: specs[m].items[kid].virtual,
						html: "<td class='full' rowspan='" + rowspan + "'>" + specs[m].items[kid].title + "</td>\r\n",
						id: specs[m].items[kid].id
					};
				}
				else {
					h[m][j] = {
						title: specs[m].items[kid].title,
						virtual: specs[m].items[kid].virtual,
						html: "",
						id: specs[m].items[kid].id
					};
				}
				n++;
				if (n == rowspan) {
					kid++;
					if (kid > specs[m].items.length - 1) {
						kid = 0;
					}
					n = 0;
				}
			}
		}

		var hh = "";
		for (var i = 0; i < newlen; i++) {
			hh += "<tr>";
			var ids = [];
			var titles = [];
			var virtuals = [];
			for (var j = 0; j < len; j++) {
				hh += h[j][i].html;
				ids.push(h[j][i].id);
				titles.push(h[j][i].title);
				virtuals.push(h[j][i].virtual);
			}
			ids = ids.join('_');
			titles = titles.join('+');
			var val = {
				id: "",
				title: titles,
			{loop $levels $level}
			{if $level['key']=='default'}
			level{$level['key']}: '',
			{else}
			level{$level['if']}: '',
			{/if}
			{/loop}
			costprice: "",
					presell: "",
					productprice: "",
					marketprice: "",
					weight: "",
					productsn: "",
					goodssn: "",
					virtual: virtuals
		};

			var val ={ id : "",title:titles,{loop $levels $level}{if $level['key']=='default'} level{$level['key']}: '',{else} level{$level['id']}: '',{/if}{/loop}costprice : "",productprice : "",marketprice : "",weight:"",productsn:"",goodssn:"",virtual:virtuals };
			if ($(".isdiscount_discounts_id_" + ids).length > 0) {
				val = {
					id: $(".isdiscount_discounts_id_" + ids + ":eq(0)").val(),
					title: titles,
				{loop $levels $level}
				{if $level['key']=='default'}
				level{$level['key']}: $(".isdiscount_discounts_{$level['key']}_" + ids + ":eq(0)").val(),
				{else}
				level{$level['id']}: $(".isdiscount_discounts_level{$level['id']}_" + ids + ":eq(0)").val(),
				{/if}
				{/loop}
				costprice: $(".isdiscount_discounts_costprice_" + ids + ":eq(0)").val(),
						productprice: $(".isdiscount_discounts_productprice_" + ids + ":eq(0)").val(),
						marketprice: $(".isdiscount_discounts_marketprice_" + ids + ":eq(0)").val(),
						presell: $(".isdiscount_discounts_presell_" + ids + ":eq(0)").val(),
						goodssn: $(".isdiscount_discounts_goodssn_" + ids + ":eq(0)").val(),
						productsn: $(".isdiscount_discounts_productsn_" + ids + ":eq(0)").val(),
						weight: $(".isdiscount_discounts_weight_" + ids + ":eq(0)").val(),
						virtual: virtuals
			}
			}

			{loop $levels $level}
			hh += '<td>'
			{if $level['key']=='default'}
			hh += '<input data-name="isdiscount_discounts_level_{$level['key']}_' + ids +'"type="text" class="form-control isdiscount_discounts_{$level['key']} isdiscount_discounts_{$level['key']}_' + ids +'" value="' +(val.level{$level['key']}=='undefined'?'':val.level{$level['key']} )+'"/>';
			{else}
			hh += '<input data-name="isdiscount_discounts_level_{$level['id']}_' + ids +'"type="text" class="form-control isdiscount_discounts_level{$level['id']} isdiscount_discounts_level{$level['id']}_' + ids +'" value="' +(val.level{$level['id']}=='undefined'?'':val.level{$level['id']} )+'"/>';
			{/if}
			hh += '</td>';
			{/loop}
			hh += '<input data-name="isdiscount_discounts_id_' + ids+'"type="hidden" class="form-control isdiscount_discounts_id isdiscount_discounts_id_' + ids +'" value="' +(val.id=='undefined'?'':val.id )+'"/>';
			hh += '<input data-name="isdiscount_discounts_ids"type="hidden" class="form-control isdiscount_discounts_ids isdiscount_discounts_ids_' + ids +'" value="' + ids +'"/>';
			hh += '<input data-name="isdiscount_discounts_title_' + ids +'"type="hidden" class="form-control isdiscount_discounts_title isdiscount_discounts_title_' + ids +'" value="' +(val.title=='undefined'?'':val.title )+'"/></td>';
			hh += '<input data-name="isdiscount_discounts_virtual_' + ids +'"type="hidden" class="form-control isdiscount_discounts_virtual isdiscount_discounts_virtual_' + ids +'" value="' +(val.virtual=='undefined'?'':val.virtual )+'"/></td>';
			hh += "</tr>";
		}
		html += hh;
		html += "</table>";
		$("#isdiscount_discounts").html(html);
	}

	function refreshCommission() {
		var commission_level = {php echo json_encode($commission_level)};
		var html = '<table class="table table-bordered table-condensed"><thead><tr class="active">';
		var specs = [];

		$(".spec_item").each(function (i) {
			var _this = $(this);

			var spec = {
				id: _this.find(".spec_id").val(),
				title: _this.find(".spec_title").val()
			};

			var items = [];
			_this.find(".spec_item_item").each(function () {
				var __this = $(this);
				var item = {
					id: __this.find(".spec_item_id").val(),
					title: __this.find(".spec_item_title").val(),
					virtual: __this.find(".spec_item_virtual").val(),
					show: __this.find(".spec_item_show").get(0).checked ? "1" : "0"
				}
				items.push(item);
			});
			spec.items = items;
			specs.push(spec);
		});
		specs.sort(function (x, y) {
			if (x.items.length > y.items.length) {
				return 1;
			}
			if (x.items.length < y.items.length) {
				return -1;
			}
		});

		var len = specs.length;
		var newlen = 1;
		var h = new Array(len);
		var rowspans = new Array(len);
		for (var i = 0; i < len; i++) {
			html += "<th>" + specs[i].title + "</th>";
			var itemlen = specs[i].items.length;
			if (itemlen <= 0) {
				itemlen = 1
			}
			;
			newlen *= itemlen;

			h[i] = new Array(newlen);
			for (var j = 0; j < newlen; j++) {
				h[i][j] = new Array();
			}
			var l = specs[i].items.length;
			rowspans[i] = 1;
			for (j = i + 1; j < len; j++) {
				rowspans[i] *= specs[j].items.length;
			}
		}

		$.each(commission_level,function (key,level) {
			html += '<th><div class=""><div style="padding-bottom:10px;text-align:center;">'+level.levelname+'</div></div></th>';
		})
		html += '</tr></thead>';

		for (var m = 0; m < len; m++) {
			var k = 0, kid = 0, n = 0;
			for (var j = 0; j < newlen; j++) {
				var rowspan = rowspans[m];
				if (j % rowspan == 0) {
					h[m][j] = {
						title: specs[m].items[kid].title,
						virtual: specs[m].items[kid].virtual,
						html: "<td class='full' rowspan='" + rowspan + "'>" + specs[m].items[kid].title + "</td>\r\n",
						id: specs[m].items[kid].id
					};
				}
				else {
					h[m][j] = {
						title: specs[m].items[kid].title,
						virtual: specs[m].items[kid].virtual,
						html: "",
						id: specs[m].items[kid].id
					};
				}
				n++;
				if (n == rowspan) {
					kid++;
					if (kid > specs[m].items.length - 1) {
						kid = 0;
					}
					n = 0;
				}
			}
		}
		var hh = "";
		for (var i = 0; i < newlen; i++) {
			hh += "<tr>";
			var ids = [];
			var titles = [];
			var virtuals = [];
			for (var j = 0; j < len; j++) {
				hh += h[j][i].html;
				ids.push(h[j][i].id);
				titles.push(h[j][i].title);
				virtuals.push(h[j][i].virtual);
			}
			ids = ids.join('_');
			titles = titles.join('+');

			var val = {
				id: "",
				title: titles,
			{loop $commission_level $level}
			{if $level["key"] == "default"}
			level{$level['key']}: '',
			{else}
			level{$level['id']}: '',
			{/if}
			{/loop}
			costprice: "",
					presell: "",
					productprice: "",
					marketprice: "",
					weight: "",
					productsn: "",
					goodssn: "",
					virtual: virtuals
		};

			var val ={ id : "",title:titles,{loop $commission_level $level} {if $level["key"] == "default"}level{$level['key']}: '',{else}level{$level['id']}: '',{/if}{/loop}costprice : "",productprice : "",marketprice : "",weight:"",productsn:"",goodssn:"",virtual:virtuals };
			{loop $commission_level $level}
			{if $level["key"] == "default"}
			var level{$level['key']} = new Array(3);
			$(".commission_{$level['key']}_"+ ids).each(function(index,val){
				level{$level['key']}[index] = val;
			})
			{else}
			var level{$level['id']} = new Array(3);
			$(".commission_level{$level['id']}_"+ ids).each(function(index,val){
				level{$level['id']}[index] = val;
			})
			{/if}
			{/loop}
			if ($(".commission_id_" + ids).length > 0) {
				val = {
					id: $(".commission_id_" + ids + ":eq(0)").val(),
					title: titles,
					costprice: $(".commission_costprice_" + ids + ":eq(0)").val(),
					presell: $(".commission_presell_" + ids + ":eq(0)").val(),
						productprice: $(".commission_productprice_" + ids + ":eq(0)").val(),
						marketprice: $(".commission_marketprice_" + ids + ":eq(0)").val(),
						goodssn: $(".commission_goodssn_" + ids + ":eq(0)").val(),
						productsn: $(".commission_productsn_" + ids + ":eq(0)").val(),
						weight: $(".commission_weight_" + ids + ":eq(0)").val(),
						virtual: virtuals
				}
			}
			{loop $commission_level $level}
			hh += '<td>';
			var level_temp = {if $level['key'] == 'default'}level{$level['key']}{else}level{$level['id']}{/if};
			if (len >= i && typeof (level_temp) != 'undefined')
			{
				if('{$level['key']}' == 'default')
				{
					for (var li = 0; li<{$shopset_level};li++)
					{
						if (typeof (level_temp[li])!= "undefined")
						{
							hh += '<input data-name="commission_level_{$level['key']}_' +ids+ '"  type="text" class="form-control commission_{$level['key']} commission_{$level['key']}_' +ids+ '" value="' +$(level_temp[li]).val()+ '" style="display:inline;width: '+96/parseInt({$shopset_level})+'%;"/> ';
						}
						else
						{
							hh += '<input data-name="commission_level_{$level['key']}_' +ids+ '"  type="text" class="form-control commission_{$level['key']} commission_{$level['key']}_' +ids+ '" value="" style="display:inline;width: '+96/parseInt({$shopset_level})+'%;"/> ';
						}
					}
				}
				else
				{
					for (var li = 0; li<{$shopset_level};li++)
					{
						if (typeof (level_temp[li])!= "undefined")
						{
							hh += '<input data-name="commission_level_{$level['id']}_' +ids+ '"  type="text" class="form-control commission_level{$level['id']} commission_level{$level['id']}_' +ids+ '" value="' +$(level_temp[li]).val()+ '" style="display:inline;width: '+96/parseInt({$shopset_level})+'%;"/> ';
						}
						else
						{
							hh += '<input data-name="commission_level_{$level['id']}_' +ids+ '"  type="text" class="form-control commission_level{$level['id']} commission_level{$level['id']}_' +ids+ '" value="" style="display:inline;width: '+96/parseInt({$shopset_level})+'%;"/> ';
						}
					}
				}
			}
			else
			{
				if('{$level['key']}' == 'default')
				{
					for (var li = 0; li<{$shopset_level};li++)
					{
						if (typeof (level_temp[li])!= "undefined")
						{
							hh += '<input data-name="commission_level_{$level['key']}_' +ids+ '"  type="text" class="form-control commission_{$level['key']} commission_{$level['key']}_' +ids+ '" value="' +$(level_temp[li]).val()+ '" style="display:inline;width: '+96/parseInt({$shopset_level})+'%;"/> ';
						}
						else
						{
							hh += '<input data-name="commission_level_{$level['key']}_' +ids+ '"  type="text" class="form-control commission_{$level['key']} commission_{$level['key']}_' +ids+ '" value="" style="display:inline;width: '+96/parseInt({$shopset_level})+'%;"/> ';
						}
					}
				}
				else
				{
					for (var li = 0; li<{$shopset_level};li++)
					{
						if (typeof (level_temp[li])!= "undefined")
						{
							hh += '<input data-name="commission_level_{$level['id']}_' +ids+ '"  type="text" class="form-control commission_level{$level['id']} commission_level{$level['id']}_' +ids+ '" value="' +$(level_temp[li]).val()+ '" style="display:inline;width: '+96/parseInt({$shopset_level})+'%;"/> ';
						}
						else
						{
							hh += '<input data-name="commission_level_{$level['id']}_' +ids+ '"  type="text" class="form-control commission_level{$level['id']} commission_level{$level['id']}_' +ids+ '" value="" style="display:inline;width: '+96/parseInt({$shopset_level})+'%;"/> ';
						}
					}
				}
			}
			hh += '</td>';
			{/loop}
			hh += '<input data-name="commission_id_' + ids+'"type="hidden" class="form-control commission_id commission_id_' + ids +'" value="' +(val.id=='undefined'?'':val.id )+'"/>';
			hh += '<input data-name="commission_ids"type="hidden" class="form-control commission_ids commission_ids_' + ids +'" value="' + ids +'"/>';
			hh += '<input data-name="commission_title_' + ids +'"type="hidden" class="form-control commission_title commission_title_' + ids +'" value="' +(val.title=='undefined'?'':val.title )+'"/></td>';
			hh += '<input data-name="commission_virtual_' + ids +'"type="hidden" class="form-control commission_virtual commission_virtual_' + ids +'" value="' +(val.virtual=='undefined'?'':val.virtual )+'"/></td>';
			hh += "</tr>";
		}
		html += hh;
		html += "</table>";
		$("#commission").html(html);
	}

function setCol(cls){
	$("."+cls).val( $("."+cls+"_all").val());
}
function showItem(obj){
	var show = $(obj).get(0).checked?"1":"0";
	$(obj).parents('.spec_item_item').find('.spec_item_show:eq(0)').val(show);
}
function nofind(){
	var img=event.srcElement;
	img.src="./resource/image/module-nopic-small.jpg";
	img.onerror=null;
}

    function choosetemp(id){
    $('#modal-module-chooestemp').modal();
    $('#modal-module-chooestemp').data("temp",id);
}
function addtemp(){
    var id = $('#modal-module-chooestemp').data("temp");
    var temp_id = $('#modal-module-chooestemp').find("select").val();
    var temp_name = $('#modal-module-chooestemp option[value='+temp_id+']').text();
    //alert(temp_id+":"+temp_name);
    $("#temp_name_"+id).val(temp_name);
    $("#temp_id_"+id).val(temp_id);
    $('#modal-module-chooestemp .close').click();
    refreshOptions()
}

function setinterval(type)
{
	var intervalfloor =$('#intervalfloor').val();
	if(intervalfloor=="")
	{
		intervalfloor=0;
	}
	intervalfloor = parseInt(intervalfloor);

	if(type=='plus')
	{
		if(intervalfloor==3)
		{
			tip.msgbox.err("最多添加三个区间价格");
			return;
		}
		intervalfloor=intervalfloor+1;
	}
	else if(type=='minus')
	{
		if(intervalfloor==0)
		{
			tip.msgbox.err("请最少添加一个区间价格");
			return;
		}
		intervalfloor=intervalfloor-1;
	}else
	{
		return;
	}

	if(intervalfloor<1)
	{

		$('#interval1').hide();
		$('#intervalnum1').val("");
		$('#intervalprice1').val("");
	}else
	{
		$('#interval1').show();
	}

	if(intervalfloor<2)
	{

		$('#interval2').hide();
		$('#intervalnum2').val("");
		$('#intervalprice2').val("");
	}else
	{
		$('#interval2').show();
	}

	if(intervalfloor<3)
	{

		$('#interval3').hide();
		$('#intervalnum3').val("");
		$('#intervalprice3').val("");
	}else
	{
		$('#interval3').show();
	}


	$('#intervalfloor').val(intervalfloor);

}


</script>